package com.lhj.crm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhj.crm.entity.Permission;
import com.lhj.crm.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author FYI
 * @since 2020-03-23
 */
@RestController
public class PermissionController {


    @Autowired
    IPermissionService permissionService;

    @GetMapping("/perm/list")
    public Map getPermList(Integer page, Integer limit ){
        Map<String,Object> map = new HashMap<String,Object>();
        //设置mybatisPlus分页
        Page<Permission> p = new Page<Permission>();
        p.setSize(limit);       //设置每页记录数
        p.setCurrent(page);     //设置当前页码
        IPage<Permission> iPage = permissionService.selectPermissionList(p);
        map.put("msg","查询情况");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }


    @GetMapping("/perm")
    public List<Permission> roles(){
        return permissionService.list();
    }



    @PostMapping("/perm")
    public Map DeptAdd(Permission permission){
        Map<String,Object> map = new HashMap<String,Object>();
        Integer add = permissionService.add(permission);
        map.put("state",add);
        return map;
    }

    @PutMapping("/perm")
    public Map RoleUpdate(Permission permission) {
        Map<String, Object> map = new HashMap<String, Object>();
        Integer update = permissionService.update(permission);
        map.put("state",update);
        return map;
    }


    @DeleteMapping("/perm/del/{permId}")
    public Map RoleDel(@PathVariable Integer permId){
        Permission permission = new Permission();
        permission.setPermId(permId);
        permission.setIsDel(1);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",permissionService.updateById(permission));
        return result;
    }


}

